package com.ellricky.newtv

import android.content.pm.ActivityInfo
import android.graphics.Color
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import com.andr.common.ui.activity.BaseActivity
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.adapter.GridItemDecoration
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.util.jump
import com.ellricky.newtv.adapter.ClassAdapter
import com.ellricky.newtv.adapter.TYPE_RECORD
import com.ellricky.baselib.entity.newtv.ClassInfo
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEListCallback
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.newtv.dialog.ClassAddDialogFragment
import com.ellricky.newtv.dialog.ClassDeleteDialogFragment
import com.ellricky.newtv.utils.CacheToken
import kotlinx.android.synthetic.main.activity_new_tv_bind_class.rv_class
import kotlinx.android.synthetic.main.activity_new_tv_bind_class.tv_back
import kotlinx.android.synthetic.main.activity_new_tv_class_manage.*

class NewTvClassManageActivity : BaseActivity<Void, BasePresenter<Void>>() {
    private val classAdapter by lazy {
        ClassAdapter()
    }

    override fun getContentLayout(): Int {
        return R.layout.activity_new_tv_class_manage
    }

    override fun setOrientation() {
        this.requestedOrientation =
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }

    override fun initUIView() {
        CacheToken.verifyToken()
        tv_back.setOnClickListener(this)
        tv_add_class.setOnClickListener(this)
        tv_delete_class.setOnClickListener(this)

        initRv()
        loadData()
    }

    private fun loadData() {
        val url = EAppUtil.getTvApiHost()
        val paramMap = mutableMapOf(
            "action" to "classList",
            "eid" to LocalCacheUtil.getTvUid()
        )
        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEListCallback<ClassInfo>(this, false) {
                override fun onSuccess(data: MutableList<ClassInfo>, msg: String?) {
                    classAdapter.setList(data)
                }
            }, this)
    }

    override fun onClickView(v: View) {
        super.onClickView(v)
        when (v.id) {
            R.id.tv_back -> finish()
            R.id.tv_add_class -> {
                showAddClassDialog()
            }
            R.id.tv_delete_class -> {
                showDeleteIcon()
            }
        }
    }

    private fun showDeleteIcon() {
        classAdapter.data.forEach {
            it.isDelete = true
        }
        classAdapter.notifyDataSetChanged()
    }

    private fun hideDeleteIcon() {
        classAdapter.data.forEach {
            it.isDelete = false
        }
        classAdapter.notifyDataSetChanged()
    }


    private fun initRv() {
        rv_class.layoutManager = GridLayoutManager(this, 6)
        rv_class.adapter = classAdapter
        rv_class.addItemDecoration(GridItemDecoration(20, 30, Color.TRANSPARENT))
        classAdapter.addChildClickViewIds(R.id.cl_class, R.id.iv_delete)
        classAdapter.setOnItemChildClickListener { _,
                                                   view,
                                                   position ->
            val data = classAdapter.data[position]

            when (view.id) {
                R.id.iv_delete -> {
                    hideDeleteIcon()
                    showDeleteConfirmDialog(data.id)
                }
                R.id.cl_class -> {
                    classAdapter.data.forEach {
                        it.isSelect = false
                    }
                    data.isSelect = !data.isSelect
                    classAdapter.notifyDataSetChanged()

                    jump<NewTvPreLessonActivity> {
                        putExtra(BaseConstant.KEY_FROM, TYPE_RECORD)
                        putExtra(BaseConstant.KEY_ID, data.id)
                    }
                }
            }
        }
    }

    private fun showDeleteConfirmDialog(classId: String) {
        val dialog = ClassDeleteDialogFragment()
        dialog.show(supportFragmentManager, "delete")
        dialog.setOnClassDeleteListener(object : ClassDeleteDialogFragment.OnClassDeleteListener {
            override fun onClassDelete() {
                deleteClass(classId)
            }
        })
    }

    private fun deleteClass(classId: String) {
        val url = EAppUtil.getTvApiHost()
        val paramMap = mutableMapOf(
            "action" to "delete",
            "class_id" to classId,
            "eid" to LocalCacheUtil.getTvUid()
        )
        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<Any>(this, false) {
                override fun onSuccess(var1: Any?, var2: String?) {
                    loadData()
                    Toast.makeText(this@NewTvClassManageActivity, "删除成功", Toast.LENGTH_SHORT).show()
                }
            }, this)
    }

    private fun showAddClassDialog() {
        val dialog = ClassAddDialogFragment()
        dialog.show(supportFragmentManager, "add")
        dialog.setOnClassAddListener(object : ClassAddDialogFragment.OnClassAddListener {
            override fun onClassAdd(name: String) {
                addClass(name)
            }
        })
    }

    private fun addClass(className: String) {
        val url = EAppUtil.getTvApiHost()
        val paramMap = mutableMapOf(
            "action" to "addClass",
            "class_name" to className,
            "eid" to LocalCacheUtil.getTvUid()
        )
        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<Any>(this, false) {
                override fun onSuccess(var1: Any?, var2: String?) {
                    loadData()
                    Toast.makeText(this@NewTvClassManageActivity, "添加成功", Toast.LENGTH_SHORT).show()
                }
            }, this)
    }
}